﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CPPEI.EBS.Proxy
{
    public static class EBSProxy
    {
        public const string FIELD_APPID = "AppId";
        public const string FIELD_APPTOKEN = "AppToken";
        public const string FIELD_NAMESPACE = "Namespace";
        public const string FIELD_EVENT = "Event";
        public const string FIELD_CALLBACK = "Callback";
        public const string FIELD_DATA = "Data";

        private static string s_address;
        private static Guid s_appId;
        private static string s_appToken;

        /// <summary>
        /// 通过配置文件类初始化
        /// </summary>
        /// <param name="configFile">配置文件地址</param>
        /// <returns></returns>
        public static bool InitWithConfig(string configFile)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(configFile);

                XmlNode xProxyAddress = xDoc.SelectSingleNode("./EBS/Proxy/Address");
                XmlNode xProxyAppId = xDoc.SelectSingleNode("./EBS/Proxy/AppId");
                XmlNode xProxyAppToken = xDoc.SelectSingleNode("./EBS/Proxy/AppToken");
                if (xProxyAddress == null)
                {
                    return false;
                }
                else
                {
                    s_address = xProxyAddress.InnerText;
                }

                if (xProxyAppId == null)
                {
                    return false;
                }
                else
                {
                    s_appId = new Guid(xProxyAppId.InnerText);
                }

                if (xProxyAppToken == null)
                {
                    return false;
                }
                else
                {
                    s_appToken = xProxyAppToken.InnerText;
                }
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="namespace"></param>
        /// <param name="event"></param>
        /// <param name="callback"></param>
        /// <returns></returns>
        public static bool Register(string @namespace, 
            string @event, 
            string callback)
        {
            string url = string.Format("{0}webapi", s_address);
            Dictionary<string, string> @params = new Dictionary<string, string>();

            @params.Add("sys:sno", "EBS");
            @params.Add("sys:act", "Register");
            @params.Add(FIELD_APPID, s_appId.ToString());
            @params.Add(FIELD_APPTOKEN, s_appToken);
            @params.Add(FIELD_NAMESPACE, @namespace);
            @params.Add(FIELD_EVENT, @event);
            @params.Add(FIELD_CALLBACK, callback);
            string resultString = HttpClient.Post(url, @params);
            return true;
        }

        public static void Unregister(string @namespace, string @event)
        {
            string url = string.Format("{0}webapi", s_address);
            Dictionary<string, string> @params = new Dictionary<string, string>();

            @params.Add("sys:sno", "EBS");
            @params.Add("sys:act", "Unegister");
            @params.Add(FIELD_APPID, s_appId.ToString());
            @params.Add(FIELD_APPTOKEN, s_appToken);
            @params.Add(FIELD_NAMESPACE, @namespace);
            @params.Add(FIELD_EVENT, @event);
            string resultString = HttpClient.Post(url, @params);
        }

        public static void Publish(string @namespace, string @event, string data)
        {
            string url = string.Format("{0}webapi", s_address);
            Dictionary<string, string> @params = new Dictionary<string, string>();

            @params.Add("sys:sno", "EBS");
            @params.Add("sys:act", "Publish");
            @params.Add(FIELD_APPID, s_appId.ToString());
            @params.Add(FIELD_APPTOKEN, s_appToken);
            @params.Add(FIELD_NAMESPACE, @namespace);
            @params.Add(FIELD_EVENT, @event);
            @params.Add(FIELD_DATA, data);
            string resultString = HttpClient.Post(url, @params);
        }
    }
}
